package com.itlong.cloud.rabbitmq.consumer;

import com.alibaba.fastjson.JSON;
import com.itlong.cloud.POJO.PO.PropertyCardPaymentRecordPO;
import com.itlong.cloud.POJO.PO.TimeTaskDeadMessageQueuePO;
import com.itlong.cloud.app.service.IAppMonthCardParkingService;
import com.itlong.cloud.constants.RabbitMQConstant;
import com.itlong.cloud.utils.node.JsonUtil;
import com.rabbitmq.client.Channel;
import org.slf4j.Logger;import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;

/**
 * <desc>
 *       保存月卡缴费记录消息队列消费者
 * </desc>
 *
 * @createDate 2018/11/08
 */
@Component
public class SaveMonthCardFeeRecordConsumer {

    private static final Logger LOG = LoggerFactory.getLogger(SaveMonthCardFeeRecordConsumer.class); //日志对象

    @Autowired
    IAppMonthCardParkingService iAppMonthCardParkingService;

    /**
     * <desc>
     *      保存月卡缴费记录消费过程
     * </desc>
     *
     * @param message 监听到的业务消息
     * @param channel 频道
     * @throws InterruptedException
     * @throws IOException
     * @author Juguang.S
     * @createDate 2018/11/08
     */
    @RabbitListener(queues = RabbitMQConstant.SAVE_MONTH_CARD_FEE_RECORD_QUEUE)
    public void process(Message message, Channel channel) throws InterruptedException,IOException {
        try {
            byte[] body = message.getBody();
            TimeTaskDeadMessageQueuePO timeTaskDeadMessageQueuePO = JsonUtil.toObject(new String(body),
                    TimeTaskDeadMessageQueuePO.class);
            PropertyCardPaymentRecordPO recordPO = JSON.parseObject(JSON.toJSONString(JSON.parseObject(timeTaskDeadMessageQueuePO.getMessageContent()).get("data")), PropertyCardPaymentRecordPO.class);
            iAppMonthCardParkingService.saveMonthCardFeeRecord(recordPO);
        } catch (Exception e) {
            channel.basicReject(message.getMessageProperties().getDeliveryTag(),false);
            LOG.error("【RabbitMQ_支付服务-保存月卡缴费记录】:消费异常",e);
        } finally {
            //手动确认消息消费成功。
            channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
        }
    }

}
